!function () {
    var $$ = Dom7;
    var GlobalUserid;
    var uploading = false;
    var gamerapp = new Framework7({
        animateNavBackIcon: true,
        modalTitle:'家庭时光',
        modalButtonOk:'确认',
        modalButtonCancel:'取消',
        template7Pages:true
    });

    var mainView = gamerapp.addView('.view-main', {
        dynamicNavbar: true
    });

    function checkuserlogin(url,redirecturl)
    {
        $$.ajax({
            contentType:'OPTIONS',
            crossDomain:true,
            method:'POST',
            url:url,
            data:{},
            dataType:'json',
            xhrFields:{"withCredentials":true},
            success:function (res) {
                // alert(JSON.stringify(res));
                if (res.code == 0){
                    window.location.href = redirecturl;
                } else {
                    GlobalUserid = parseInt(res.data.uid);
                }
            },
            error:function(res){
                // alert(JSON.stringify(res));
                window.location.href = redirecturl;
            }
        });
    }
    function startupload()
    {
        var html = '<div class="flex-row flex-nowrap flex-justify-center flex-align-center loader-wrap">' +
            '<i class="f7-icons color-green hidden succ" style="margin-right: 15px;width: 25px;height: 25px;">check</i>' +
            '<i class="f7-icons color-red hidden fail" style="margin-right: 15px;width: 25px;height: 25px;">close</i>' +
            '<span class="preloader proc" style="margin-right: 15px;width: 25px;height: 25px;"></span>' +
            '<span class="info bold">提交内容</span>' +
            '</div>';
        gamerapp.modal({text:html});
    }
    function completeupload()
    {
        var succ = $$('.loader-wrap .succ');
        var info = $$('.loader-wrap .info');
        var proc = $$('.loader-wrap .proc');
        proc.addClass('hidden');
        succ.removeClass('hidden');
        info.html('上传成功');

        setTimeout(function () {
            gamerapp.closeModal();
        },1000);
    }
    function failupload()
    {
        var fail = $$('.loader-wrap .fail');
        var info = $$('.loader-wrap .info');
        var proc = $$('.loader-wrap .proc');
        proc.addClass('hidden');
        fail.removeClass('hidden');
        info.html('系统繁忙，请重试');

        setTimeout(function () {
            gamerapp.closeModal();
        },2000)
    }

    checkuserlogin('http://api.nldou.com/ucenter/checkUserLogin','http://api.nldou.com/ucenter/weixinlogin?pid=6329');

    $$.ajax({
        url:'http://api.nldou.com/weixinapi/init_wx_jsapi',
        method:"GET",
        data: {fromUrl:window.location.href},
        dataType:'json',
        success: function (res) {
            if(res.code==1){
                //权限申请
                wx.config({
                    debug: false,
                    appId: res.data.appid,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage']
                });
            }
        }
    });
    wx.ready(function () {
        wx.onMenuShareTimeline({
            title:'家庭时光游戏引导师申请表',
            link: 'http://api.nldou.com/ucenter/weixinlogin?pid=6329',
            imgUrl: 'http://wap.nldou.com/gamer/img/1.jpg',
            success: function () {return true;},
            cancel: function () {return true;}
        });
        wx.onMenuShareAppMessage({
            title: '家庭时光游戏引导师申请表',
            desc: '家庭时光游戏引导师申请表，通过审核后即可参加课程！',
            link: 'http://api.nldou.com/ucenter/weixinlogin?pid=6329',
            imgUrl: 'http://wap.nldou.com/gamer/img/1.jpg',
            type: 'link',
            success: function () {return true;},
            cancel: function () {return true;}
        });
        wx.onMenuShareQQ({
            title: '家庭时光游戏引导师申请表',
            desc: '家庭时光游戏引导师申请表，通过审核后即可参加课程！',
            link: 'http://api.nldou.com/ucenter/weixinlogin?pid=6329',
            imgUrl: 'http://wap.nldou.com/gamer/img/1.jpg',
            type: 'link',
            success: function () {return true;},
            cancel: function () {return true;}
        });
        wx.onMenuShareWeibo({
            title: '家庭时光游戏引导师申请表',
            desc: '家庭时光游戏引导师申请表，通过审核后即可参加课程！',
            link: 'http://api.nldou.com/ucenter/weixinlogin?pid=6329',
            imgUrl: 'http://wap.nldou.com/gamer/img/1.jpg',
            success: function () {return true;},
            cancel: function () {return true;}
        });
        wx.onMenuShareQZone({
            title: '家庭时光游戏引导师申请表',
            desc: '家庭时光游戏引导师申请表，通过审核后即可参加课程！',
            link: 'http://api.nldou.com/ucenter/weixinlogin?pid=6329',
            imgUrl: 'http://wap.nldou.com/gamer/img/1.jpg',
            success: function () {return true;},
            cancel: function () {return true;}
        });
    });

    gamerapp.CitylistTemplate = Template7.compile($$('script#Citylist').html());
    gamerapp.ArealistTemplate = Template7.compile($$('script#Arealist').html());

    $$.ajax({
        method:'GET',
        url:'http://api.nldou.com/gamer/getCityList',
        data:{},
        dataType:'json',
        success:function (res) {
            //console.log('获取城市列表',res);
            if (res.code == 1){
                var html = gamerapp.CitylistTemplate(res.data);
                $$('select[name="jobcity"]').html(html);
                $$('select[name="livecity"]').html(html);
            }
        }
    });

    $$('select[name="jobcity"]').on('change',function () {
        var city = $$(this).val();
        $$.ajax({
            method:'POST',
            url:'http://api.nldou.com/gamer/getAreaListBycity',
            data:{cityid:city},
            dataType:'json',
            success:function (res) {
                //console.log('获取区域列表',res);
                if (res.code == 1){
                    var html = gamerapp.ArealistTemplate(res.data);
                    $$('select[name="jobarea"]').html(html);
                }
            }
        });
    });
    $$('select[name="livecity"]').on('change',function () {
        var city = $$(this).val();
        $$.ajax({
            method:'POST',
            url:'http://api.nldou.com/gamer/getAreaListBycity',
            data:{cityid:city},
            dataType:'json',
            success:function (res) {
                //console.log('获取区域列表',res);
                if (res.code == 1){
                    var html = gamerapp.ArealistTemplate(res.data);
                    $$('select[name="livearea"]').html(html);
                }
            }
        });
    });
    $$('.sub-btn').on('click',function () {
        if (uploading === false){
            uploading = true;
            var data = gamerapp.formToData('#myform');
            var userName = data['userName'].replace(/\s/g,"");
            var wc = userName.length;
            if (wc == 0) {gamerapp.alert('请输入姓名','');uploading = false;return false;}

            var userGender = data['userGender'];
            wc = userGender.length;
            if (wc == 0) {gamerapp.alert('请选择性别','');uploading = false;return false;}

            var telNum = data['telNum'];
            if(!(/^1\d{10}$/).test(telNum)){gamerapp.alert('请输入正确的11位手机号','');uploading = false;return false;}

            var wx_num = data['wx_num'].replace(/\s/g,"");
            wc = wx_num.length;
            if (wc == 0) {gamerapp.alert('请输入微信号','');uploading = false;return false;}

            var email = data['email'].replace(/\s/g,"");
            wc = email.length;
            if (wc == 0) {gamerapp.alert('请输入邮箱','');uploading = false;return false;}
            if (email.indexOf('@') == -1) {gamerapp.alert('请输入正确的邮箱','');uploading = false;return false;}

            var eduLevel = data['eduLevel'];
            wc = eduLevel.length;
            if (wc == 0) {gamerapp.alert('请选择学历','');uploading = false;return false;}

            var career = data['career'].replace(/\s/g,"");
            wc = career.length;
            if (wc == 0) {gamerapp.alert('请输入职业','');uploading = false;return false;}

            var careerstatus = data['careerstatus'];
            wc = careerstatus.length;
            if (wc == 0) {gamerapp.alert('请选择职业状态','');uploading = false;return false;}

            var cert = data['cert'].replace(/\n|\r\n|\r/g,"<br>").replace(/\s/g,"");

            var jobcity = data['jobcity'];
            wc = jobcity.length;
            if (wc == 0) {gamerapp.alert('请选择工作城市','');uploading = false;return false;}

            var jobarea = data['jobarea'];
            wc = jobarea.length;
            if (wc == 0) {gamerapp.alert('请选择工作区域','');uploading = false;return false;}

            var marriage = data['marriage'];
            wc = marriage.length;
            if (wc == 0) {gamerapp.alert('请选择婚姻状况','');uploading = false;return false;}

            var childage = data['childage'];
            if(!(/^[0-9]{1,2}$/).test(childage)) {gamerapp.alert('请输入正确孩子年龄','');uploading = false;return false;}

            var livecity = data['livecity'];
            wc = livecity.length;
            if (wc == 0) {gamerapp.alert('请选择居住城市','');uploading = false;return false;}

            var livearea = data['livearea'];
            wc = livearea.length;
            if (wc == 0) {gamerapp.alert('请选择居住区域','');uploading = false;return false;}

            var freetime = data['freetime'];
            wc = freetime.length;
            if (wc == 0) {gamerapp.alert('请选择空余时间','');uploading = false;return false;}

            var experience = data['experience'].replace(/\n|\r\n|\r/g,"<br>").replace(/\s/g,"");
            var hobby = data['hobby'].replace(/\n|\r\n|\r/g,"<br>").replace(/\s/g,"");

            var reason = data['reason'].replace(/\n|\r\n|\r/g,"<br>").replace(/\s/g,"");
            wc = reason.length;
            if (wc == 0) {gamerapp.alert('请输入报名原因','');uploading = false;return false;}

            var hope = data['hope'].replace(/\n|\r\n|\r/g,"<br>").replace(/\s/g,"");

            var result = {'uid':GlobalUserid,'userName':userName,'userGender':userGender,'telNum':telNum,'wx_num':wx_num,'email':email,'eduLevel':eduLevel,
                'career':career,'careerstatus':careerstatus,'cert':cert,'jobcity':jobcity,'jobarea':jobarea,
                'marriage':marriage,'childage':childage,'livecity':livecity,'livearea':livearea,
                'freetime':freetime,'experience':experience,'hobby':hobby,'reason':reason,'hope':hope};
            //console.log(result);
            startupload();
            $$.ajax({
                contentType:'OPTIONS',
                crossDomain:true,
                method:'POST',
                url:'http://api.nldou.com/gamer/register',
                data:result,
                dataType:'json',
                success:function (res) {
                    //console.log('上传成功',res);
                    if (res.code == 0){
                        failupload();
                        uploading = false;
                    } else {
                        completeupload();
                        uploading = false;
                        mainView.router.load({url:'registerok.html'});
                    }
                },
                error:function (res) {
                    //console.log('上传失败',res);
                    failupload();
                    uploading = false;
                }
            });
        }
    });
}();